package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.EnterpriseSupplier;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.domain.dto.SupplierEnterpriseDTO;
import com.ruoyi.system.mapper.EnterpriseSupplierMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SupplierEnterpriseMapper;
import com.ruoyi.system.domain.SupplierEnterprise;
import com.ruoyi.system.service.ISupplierEnterpriseService;

import javax.annotation.Resource;

/**
 * 供应商对应企业关系Service业务层处理
 *
 * @author 协同
 * @date 2022-04-18
 */
@Service
public class SupplierEnterpriseServiceImpl implements ISupplierEnterpriseService {
    @Resource
    private SupplierEnterpriseMapper supplierEnterpriseMapper;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SysUserRoleMapper sysUserRoleMapper;
    @Resource
    private EnterpriseSupplierMapper enterpriseSupplierMapper;

    /**
     * 查询供应商对应企业关系
     *
     * @param id 供应商对应企业关系主键
     * @return 供应商对应企业关系
     */
    @Override
    public SupplierEnterprise selectSupplierEnterpriseById(Long id) {
        return supplierEnterpriseMapper.selectSupplierEnterpriseById(id);
    }

    /**
     * 查询供应商对应企业关系列表
     *
     * @param supplierEnterprise 供应商对应企业关系
     * @return 供应商对应企业关系
     */
    @Override
    public List<SupplierEnterprise> selectSupplierEnterpriseList(SupplierEnterprise supplierEnterprise) {
        supplierEnterprise.setSupplierId(SecurityUtils.getLoginUser().getUserId());
        return supplierEnterpriseMapper.selectSupplierEnterpriseList(supplierEnterprise);
    }

    /**
     * 新增供应商对应企业关系
     *
     * @param dto 供应商对应企业关系
     * @return 结果
     */
    @Override
    public String insertSupplierEnterprise(SupplierEnterpriseDTO dto) {
        String msg;
        if (sysUserMapper.checkUserNameUnique(dto.getEnterpriseName()) > 0) {
            msg = "新增企业'" + dto.getEnterpriseName() + "'失败，登录账号已存在";
        } else {
            SysUser user = new SysUser();
            user.setUserName(dto.getEnterpriseName());
            user.setNickName(dto.getEnterpriseName());
            user.setCreateBy(SecurityUtils.getLoginUser().getUsername());
            user.setPassword(SecurityUtils.encryptPassword("123456"));
            user.setDeptId(101L);
            // 新增供应商信息
            sysUserMapper.insertUser(user);
            // 新增供应商与角色关联
            SysUserRole ur = new SysUserRole();
            ur.setUserId(user.getUserId());
            ur.setRoleId(100L);
            List<SysUserRole> list = new ArrayList<>();
            list.add(ur);
            sysUserRoleMapper.batchUserRole(list);

            // 供应商对应企业关系
            SupplierEnterprise supplierEnterprise = new SupplierEnterprise();
            supplierEnterprise.setEnterpriseId(user.getUserId());
            supplierEnterprise.setSupplierId(SecurityUtils.getLoginUser().getUserId());
            supplierEnterprise.setMark(dto.getMark());
            supplierEnterprise.setStability(dto.getStability());
            supplierEnterpriseMapper.insertSupplierEnterprise(supplierEnterprise);

            // 企业对应合作商关系
            EnterpriseSupplier enterpriseSupplier = new EnterpriseSupplier();
            enterpriseSupplier.setEnterpriseId(user.getUserId());
            enterpriseSupplier.setSupplierId(SecurityUtils.getLoginUser().getUserId());
            enterpriseSupplierMapper.insertEnterpriseSupplier(enterpriseSupplier);

            msg = "新增企业成功";
        }
        return msg;
    }

    /**
     * 修改供应商对应企业关系
     *
     * @param supplierEnterprise 供应商对应企业关系
     * @return 结果
     */
    @Override
    public int updateSupplierEnterprise(SupplierEnterprise supplierEnterprise) {
        supplierEnterprise.setUpdateTime(DateUtils.getNowDate());
        return supplierEnterpriseMapper.updateSupplierEnterprise(supplierEnterprise);
    }

    /**
     * 批量删除供应商对应企业关系
     *
     * @param ids 需要删除的供应商对应企业关系主键
     * @return 结果
     */
    @Override
    public int deleteSupplierEnterpriseByIds(Long[] ids) {
        return supplierEnterpriseMapper.deleteSupplierEnterpriseByIds(ids);
    }

    /**
     * 删除供应商对应企业关系信息
     *
     * @param id 供应商对应企业关系主键
     * @return 结果
     */
    @Override
    public int deleteSupplierEnterpriseById(Long id) {
        return supplierEnterpriseMapper.deleteSupplierEnterpriseById(id);
    }
}
